package com.xiangwei.controller;

/**
 * Portal 2015
 * @date 2015-7-2
 * @author tangshashan
 * @version V0.1
 * @brief 统计表管理类
 */
import java.io.IOException;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.xiangwei.entity.Summary;
import com.xiangwei.entity.Vendor;
import com.xiangwei.service.IGodService;
import com.xiangwei.service.IProductService;
import com.xiangwei.service.IQrcodeService;
import com.xiangwei.service.IScanrecordService;
import com.xiangwei.service.ISummaryService;
import com.xiangwei.service.IVendorService;
import com.xiangwei.util.Common;
import com.xiangwei.util.Const;
import com.xiangwei.util.DateUtils;
import com.xiangwei.util.StringUtils;
import com.xiangwei.view.EchartsView;

@Controller
@RequestMapping(value = "admin")
public class StatisticController {
	private static final Logger logger = LoggerFactory
			.getLogger(StatisticController.class);
	@Resource
	private IProductService productService;
	@Resource
	private IVendorService vendorService;
	@Resource
	private IQrcodeService qrcodeService;
	@Resource
	private ISummaryService summaryService;
	@Resource
	private IGodService godService;
	@Resource
	private IScanrecordService scanrecordService;



	/**
	 * 跳转到二维码扫码统计页面
	 * 
	 * @return
	 */
	@RequestMapping(value = "qrcodeStatistic", method = RequestMethod.GET)
	public String qrcodeStatistic(Model model) {
		// 初始化客户信息取得
		List<Vendor> vendorList = vendorService.findAllVendors();
		model.addAttribute("vendorList", vendorList);
		return "admin/qrcodeStatistic";
	}

	/**
	 * 跳转到用户增长统计页面
	 * 
	 * @return
	 */
	@RequestMapping(value = "godStatistic", method = RequestMethod.GET)
	public String godStatistic(Model model) {
		// 初始化客户信息取得

		List<Vendor> vendorList = vendorService.findAllVendors();
		model.addAttribute("vendorList", vendorList);				
		return "admin/godStatistic";
	}



	/**
	 * 二维码扫描统计图表数据生成
	 * @param session
	 * @param response
	 * @throws IOException
	 */
	@RequestMapping(value = "qrcodeECharts", method = RequestMethod.POST)
	public void qrcodeECharts(String id, String startDate,
			String endDate, HttpServletResponse response, HttpSession session) {
		Map<String, Object> paraMap = new HashMap<String, Object>();
		paraMap.put("productId", id);

		//设置起始日期和结束日期
		if(!StringUtils.isEmpty(startDate)){
			paraMap.put("startDate", DateUtils.stringToDate(startDate, DateUtils.PATTEN_YMD));
		}else{
			paraMap.put("startDate", DateUtils.getCurrentDate());
		}
		if(!StringUtils.isEmpty(endDate)){
			paraMap.put("endDate", DateUtils.stringToDate(endDate, DateUtils.PATTEN_YMD));
		}else{
			paraMap.put("endDate", DateUtils.getCurrentDate());
		}
		// 统计图表数据取得
		EchartsView ehv = summaryService.qrcodeECharts(paraMap);
		// 数据返回至页面
		try {
			response.getWriter().write(JSON.toJSON(ehv).toString());
		} catch (IOException e) {
			logger.error(e.getMessage());
		}
	}

	/**
	 * 用户增长统计图表数据生成
	 * @param session
	 * @param response
	 * @throws IOException
	 */
	@RequestMapping(value = "godECharts", method = RequestMethod.POST)
	public void godECharts(String id, String startDate,
			String endDate, HttpServletResponse response, HttpSession session) {
		
		Map<String, Object> paraMap = new HashMap<String, Object>();
		if(!StringUtils.isEmpty(startDate)){
			paraMap.put("startDate", DateUtils.stringToDate(startDate, DateUtils.PATTEN_YMD));
		}else{
			paraMap.put("startDate", DateUtils.getCurrentDate());
		}
		if(!StringUtils.isEmpty(endDate)){
			paraMap.put("endDate", DateUtils.stringToDate(endDate, DateUtils.PATTEN_YMD));
		}else{
			paraMap.put("endDate", DateUtils.getCurrentDate());
		}
		paraMap.put("productId", id);
		EchartsView ehv = summaryService.godECharts(paraMap);
		try {
			response.getWriter().write(JSON.toJSON(ehv).toString());
		} catch (IOException e) {
			logger.error(e.getMessage());
		}
	
	}

	
	@RequestMapping(value = "statistic", method = RequestMethod.POST)
	public void getGodStatisticView(String id,HttpServletResponse response){
		if(!Common.isEmptyString(id)){
			logger.error("method getGodStatisticView 传入参数为空");
			return ;
		}

		Long qrcodeCount = (long) 0;
		Long scanrecordCount = (long) 0;

		Integer yesterdayScanrecordCount = 0;
		Map<String,Object> map = new HashMap<String, Object>();
		
		map.put("productId", id);
		Date endDate = DateUtils.addDay(new Date(), -1);
		Date startDate = endDate;
		map.put("endDate", endDate);
		map.put("startDate", startDate);
		

		String tableName1 = "qlm_fcrm_qrcode_"+id;
		String tableName2 = "qlm_portal_scanrecord_"+id;

		if(qrcodeService.existTable(tableName1)>0){
			qrcodeCount = (long) qrcodeService.getMaxId(map);			
		}
		if(qrcodeService.existTable(tableName2)>0){
			scanrecordCount = (long) scanrecordService.getScanrecordCount(map);

		}
		
		Summary summary = new Summary();

		summary.setQrcodeCount(qrcodeCount);
		summary.setScanQrcodeCount(scanrecordCount);
		summary.setYesterdayScanrecordCount(yesterdayScanrecordCount);
		
		try {
			response.getWriter().print(JSONObject.toJSON(summary));
		} catch (IOException e) {
			logger.error(e.getMessage());
		}
	}
	
	
	/**
	 * 用户增长统计图表数据生成
	 * @param session
	 * @param response
	 * @throws IOException
	 */
	@RequestMapping(value = "godAddECharts", method = RequestMethod.POST)
	public void godAddECharts(String id, String days, HttpServletResponse response, HttpSession session) {
		Map<String, Object> paraMap = new HashMap<String, Object>();
		if(!Common.isEmptyString(days) || !Common.isEmptyString(id)){
			logger.error("method godAddECharts 传入参数为空");
			return;
		}		
		Integer num = -Integer.parseInt(days);
		paraMap.put("endDate", DateUtils.getCurrentDate());
		paraMap.put("startDate", DateUtils.addDay(DateUtils.getCurrentDate(), num+1));
		paraMap.put("productId", id);
		EchartsView ehv = summaryService.godECharts(paraMap);
		try {
			response.getWriter().write(JSON.toJSON(ehv).toString());
		} catch (IOException e) {
			logger.error(e.getMessage());
		}
	
	}


	/**
	 * 二维码扫描统计图表数据生成
	 * @param session
	 * @param response
	 * @throws IOException
	 */
	@RequestMapping(value = "scanAddECharts", method = RequestMethod.POST)
	public void scanAddECharts(String id, String days, HttpServletResponse response, HttpSession session) {
		Map<String, Object> paraMap = new HashMap<String, Object>();

		if(!Common.isEmptyString(days) || !Common.isEmptyString(id)){
			logger.error("method scanAddECharts 传入参数为空");
			return;
		}		
		Integer num = -Integer.parseInt(days);
		paraMap.put("endDate", DateUtils.getCurrentDate());
		paraMap.put("startDate", DateUtils.addDay(DateUtils.getCurrentDate(), num+1));
		paraMap.put("productId", id);
		// 统计图表数据取得
		EchartsView ehv = summaryService.qrcodeECharts(paraMap);
		// 数据返回至页面
		try {
			response.getWriter().write(JSON.toJSON(ehv).toString());
		} catch (IOException e) {
			logger.error(e.getMessage());
		}
	}
	

}
